<?php
/* 
 * class.mycityBase.php
 * 我的家园基础公共类
 * @version 0.1
 * @author samuel
 * @date 2009-05-23
 */
class mycityBase {

    public $core ;
    public $params;
    public $error;

    function  __construct() {
        $this->core = $GLOBALS['CORE'];
        $this->params = $this->core->params;
        $this->error = "";
        $this->prompt = "";
    }

    //错误跳转
    function jumpError($error="", $pagetitle="", $backurl="" ){
    	$errors = $error ? $error : $this->error;
    	$page_title = $pagetitle ? $pagetitle : "JustFun-错误提示";
    	$backurl = $backurl ? htmlspecialchars($backurl) : htmlspecialchars($_SERVER['HTTP_REFERER']);
    	$content = $errors;
    	//header("Content-type: text/vnd.wap.wml");
    	$this->core->tpl->assign('content', $content);
	    $this->core->tpl->assign('page_title', $page_title);
	    $this->core->tpl->assign('backurl', $backurl);
	    $this->core->tpl->display('info.wml');
    }
    
    //提示跳转
    function jumpPrompt($prompt="", $pagetitle="", $backurl=""){
        $prompts = $prompt ? $prompt : $this->prompt;
    	$page_title = $pagetitle ? $pagetitle : "JustFun-信息提示";
    	$backurl = $backurl ? htmlspecialchars($backurl) : htmlspecialchars($_SERVER['HTTP_REFERER']);
    	//header("Content-type: text/vnd.wap.wml");
    	$this->core->tpl->assign('content', $prompts);
	    $this->core->tpl->assign('page_title', $page_title);
	    $this->core->tpl->assign('backurl', $backurl);
	    $this->core->tpl->display('info.wml');
    }
    
    //商家评分表
    function getSellerGradeIcon($grade, $counts){
    	$icon = "<img src='' alt='星星分数' />";
    	$str = "";
    	if($counts == 0) return;
    	$ave_grade = round($grade/$counts);
    	for($i = 1; $i <=$ave_grade; $i++){
    	    $str .= $icon;
    	}
    	return $str;
    }
    
    //获取某商家的评分统计与用户评分情况
    function getSellerGrades($sellerid='', $userid='', $count=false){
    	if(!$sellerid && !$userid) return false;
    	if($sellerid) $append_sid = " AND `SellerID`='".$sellerid."'";
    	if($userid) $append_uid = " AND `UserID`='".$userid."'";
    	if($count){
    		$sql = "SELECT count(`SellerGradeID`) FROM `fun_seller_grade` WHERE 1 " . $append_sid . $append_uid;
    		$counts = $this->core->db->get_var($sql);
    		return $counts;
    	}else{
    		$sql = "SELECT * FROM `fun_seller_grade` WHERE 1 " . $append_sid . $append_uid;
    		$res = $this->core->db->get_results($sql);
    		$arr_infos = array();
    		if(!empty($res)){
                foreach($res as $key => $row){
                    foreach($row as $k => $v){
                        $arr_infos[$key][$k] = $v;
                    }
                }
            }
            if($sellerid && $userid) return $arr_infos[0];
            return $arr_infos;
    	}
    }
    
    //获取用户好友
    function getUserFriends($user_id, $status="1", $count=false, $arr_limit=""){
        if(!$user_id) return false;
        if($user_id) $append_userid = " AND a.`UserID`='".$user_id."'";
        if($status == "1" || $status == "0"){
            $append_status = " AND `Status` = '".$status."'";
        }
        if($arr_limit) $append_limit = " LIMIT ".$arr_limit['offset'].", ".$arr_limit['pagesize'];
        if($count){
            $sql = "SELECT count(`UserFid`) FROM `fun_user_friend` WHERE 1 AND `UserID`='".$user_id."'" . $append_status;
            $counts = $this->core->db->get_var($sql);
            return $counts;
        }else{
            $sql = "SELECT a.*, b.* FROM `fun_user_friend` as a LEFT JOIN `fun_user` as b ON a.`UserFriendID`=b.`UserID` WHERE 1 " . $append_userid . $append_status . $append_limit;
            //echo $sql;
            $res = $this->core->db->get_results($sql);
            $arr_infos = array();
    		if(!empty($res)){
                foreach($res as $key => $row){
                    foreach($row as $k => $v){
                        $arr_infos[$key][$k] = $v;
                    }
                }
            }
            return $arr_infos;
        } 
    }
    
    
    
    //获取商家特色
    function getSellerPics($sellerid, $type="spe", $arr_limit=""){
        if(!$sellerid) return false;
        if($arr_limit) $append_limit = " LIMIT ".$arr_limit['offset']." ,".$arr_limit['pagesize'];
        if($type == "spe"){
            $append = " AND `SellerPicClass` = '1'";
        }else{
            $append = " AND `SellerPicClass` = '2'";
        }
        $sql = "SELECT * FROM `fun_seller_pic` WHERE 1 AND `SellerID`='".$sellerid."'" . $append.$append_limit;
        $res = $this->core->db->get_results($sql);
        $arr_infos = array();
		if(!empty($res)){
            foreach($res as $key => $row){
                foreach($row as $k => $v){
                    $arr_infos[$key][$k] = $v;
                }
            }
        }
        return $arr_infos;
    }
    
    //获取商家评价
    function getSellerOpinion($sellerid, $userid="", $arr_limit=""){
        if(!$sellerid) return false;
        if($userid) $append = " AND a.`UserID` IN (".$userid.")";
        if($arr_limit) $append_limit = " LIMIT ".$arr_limit['offset']." ,".$arr_limit['pagesize'];
        $sql = "SELECT * FROM `fun_seller_opinion` as a LEFT JOIN `fun_user` as b ON a.`UserID`=b.`UserID` WHERE 1 AND a.`SellerID`='".$sellerid."'" . $append.$append_limit;
        $res = $this->core->db->get_results($sql);
        $arr_infos = array();
		if(!empty($res)){
            foreach($res as $key => $row){
                foreach($row as $k => $v){
                    $arr_infos[$key][$k] = $v;
                }
            }
        }

        return $arr_infos;
    }
    
    //获取商家停车位信息
    function getSellerParking($sellerid, $arr_filter="", $arr_orderby="", $arr_limit=""){
        if(!$sellerid) return false;
        if($arr_filter) $append_where = " AND `".$arr_filter['attr']."`".$arr_filter['oper']."'".$arr_filter['value']."'";
        if($arr_orderby) $append_order = " ORDER BY `".$arr_orderby['attr']."` ".$arr_orderby['order'];
        if($arr_limit) $append_limit = " LIMIT ".$arr_limit['offset']." ,".$arr_limit['pagesize'];
        $sql = "SELECT * FROM `fun_seller_parking` WHERE 1 AND `SellerID`='".$sellerid."'".$append_where. $append_order . $append_limit;

        $res = $this->core->db->get_results($sql);
        $arr_infos = array();
		if(!empty($res)){
            foreach($res as $key => $row){
                foreach($row as $k => $v){
                    $arr_infos[$key][$k] = $v;
                }
            }
        }

        return $arr_infos;
    }
    
    //获取套餐信息
    function getSetInfo($setid="", $user_id=""){
        if(!$setid && !$user_id) return false;
        if($setid) $append_sid = " AND `SellerSetID`='".$setid."'";
        if($user_id) $append_uid = " AND `UserID`='".$user_id."'";
        $sql = "SELECT * FROM `fun_seller_set` WHERE 1 " . $append_sid . $append_uid;
        $res = $this->core->db->get_results($sql);
        $arr_infos = array();
		if(!empty($res)){
            foreach($res as $key => $row){
                foreach($row as $k => $v){
                    $arr_infos[$key][$k] = $v;
                }
            }
        }
        if($setid) return $arr_infos[0];
        return $arr_infos;
    }
    
    //获取具体套餐内容
    function getSetContent($setid){
        if(!$setid) return false;
        $sql = "SELECT * FROM `fun_set` WHERE 1 AND `SellerSetID`='".$setid."' ORDER BY `SellerIDNo` ASC";
        $res = $this->core->db->get_results($sql);
        $arr_infos = array();
		if(!empty($res)){
            foreach($res as $key => $row){
                foreach($row as $k => $v){
                    $arr_infos[$key][$k] = $v;
                }
            }
        }
        
        return $arr_infos;
    }
}
?>
